package org.cloud.admin.auth.controller;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.ArrayUtils;
import org.cloud.admin.auth.holder.AppHolder;
import org.cloud.admin.auth.service.SysUserRoleService;
import org.cloud.admin.auth.service.SysUserService;
import org.cloud.admin.auth.vo.PasswordVo;
import org.cloud.common.model.R;
import org.cloud.common.util.AssertUtils;
import org.cloud.common.util.DigestUtils;
import org.cloud.common.util.ValidatorUtils;
import org.cloud.module.common.entity.SysUserEntity;
import org.cloud.module.common.group.AddGroup;
import org.cloud.module.common.group.UpdateGroup;
import org.cloud.module.common.util.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 系统用户
 * @author wangyuan
 * @since 2019年6月4日 下午4:31:44
 *
 */
@RestController
@RequestMapping("/sys/user")
public class SysUserController {
	
	@Autowired
	private SysUserService sysUserService;
	
	@Autowired
	private SysUserRoleService sysUserRoleService;


	/**
	 * 所有用户列表
	 */
	@GetMapping("/list")
	public R list(@RequestParam Map<String, Object> params){
		PageUtils page = sysUserService.queryPage(params);

		return R.ok().put("page", page);
	}
	
	/**
	 * 获取登录的用户信息
	 */
	@GetMapping("/info")
	public R info(){
		return R.ok().put("user", sysUserService.getById(AppHolder.getLoginedUserId()));
	}
	
	/**
	 * 修改登录用户密码
	 */
	@PostMapping("/password")
	public R password(@ModelAttribute PasswordVo form) {
		AssertUtils.isBlank(form.getNewPassword(), "新密码不为能空");
		
		//sha256加密
		String password = DigestUtils.sha256Hex(form.getPassword(), AppHolder.getLoginedUser().getSalt());
		//sha256加密
		String newPassword = DigestUtils.sha256Hex(form.getNewPassword(), AppHolder.getLoginedUser().getSalt());
				
		//更新密码
		boolean flag = sysUserService.updatePassword(AppHolder.getLoginedUserId(), password, newPassword);
		if(!flag){
			return R.error("原密码不正确");
		}
		
		return R.ok();
	}
	
	/**
	 * 用户信息
	 */
	@GetMapping("/info/{userId}")
	public R info(@PathVariable("userId") Long userId){
		SysUserEntity user = sysUserService.getById(userId);
		
		user.setPassword(null);
		user.setSalt(null);
		user.setStatus(null);
		user.setCreatedUser(null);
		user.setCreatedTime(null);
		user.setUpdatedUser(null);
		user.setUpdatedTime(null);
		
		//获取用户所属的角色列表
		List<Long> roleIdList = sysUserRoleService.queryRoleIdList(userId);
		user.setRoleIdList(roleIdList);
		
		return R.ok().put("user", user);
	}
	
	/**
	 * 保存用户
	 */
	@PostMapping("/save")
	public R save(@ModelAttribute SysUserEntity user){
		ValidatorUtils.validateEntity(user, AddGroup.class);
		
		user.setCreatedUser(String.valueOf(AppHolder.getLoginedUserId()));
		user.setUpdatedUser(String.valueOf(AppHolder.getLoginedUserId()));
		sysUserService.saveUser(user);
		
		return R.ok();
	}
	
	/**
	 * 修改用户
	 */
	@PostMapping("/update")
	public R update(@ModelAttribute SysUserEntity user){
		ValidatorUtils.validateEntity(user, UpdateGroup.class);
		
		sysUserService.update(user);
		
		return R.ok();
	}
	
	/**
	 * 删除用户
	 */
	@PostMapping("/delete")
	public R delete(@RequestParam String userIds){
	    String[] userIdArr = userIds.split(",");
	    Long[] userIdLongArr = new Long[userIdArr.length];
	    for(int i = 0;i < userIdArr.length;i++) {
	        userIdLongArr[i] = Long.parseLong(userIdArr[i]);
	    }
	    
		if(ArrayUtils.contains(userIdLongArr, AppHolder.getLoginedUserId())){
			return R.error("当前用户不能删除");
		}
		
		sysUserService.deleteBatch(userIdLongArr);
		
		return R.ok();
	}
}
